package com.czl.module_work.viewModel

import android.os.Bundle
import androidx.databinding.ObservableField
import com.blankj.utilcode.util.ToastUtils
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.config.AppConstants
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.ApplyBean
import com.czl.base.data.bean.ApplyDetailBean
import com.czl.base.event.LiveBusCenter
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper

class AppointmentDetailViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {

    val auditStatus = ObservableField<Boolean>(false)
    val selectCodeStatus = ObservableField(false)
    val inviteStatus = ObservableField(false)

    val guestNo = ObservableField<String>()
    val approveStatus = ObservableField<String>()
    val createTime = ObservableField<String>()
    val reservationTime = ObservableField<String>()
    val guestUserType = ObservableField<String>()
    val guestCardType = ObservableField<String>()
    val identity = ObservableField<String>()
    val guestName = ObservableField<String>()
    val guestMobile = ObservableField<String>()
    val cardNo = ObservableField<String>()
    val guestFaceUrl = ObservableField<String>()
    val visitName = ObservableField<String>()
    val visitMobile = ObservableField<String>()
    val unitName = ObservableField<String>()
    val guestPurpose = ObservableField<String>()
    var applyId = ""


    val uc = UiChangeEvent()

    class UiChangeEvent {
        val showAuditEvent: SingleLiveEvent<String> = SingleLiveEvent()
    }

    val btnMenu1Click: BindingCommand<Void> = BindingCommand(BindingAction {
        finish()
        startContainerActivity(AppConstants.Router.Work.F_WORK_USER_INVITE)
    })
    val btnMenu2Click: BindingCommand<Void> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.Work.F_WORK_MY_INVITE, Bundle().apply {
            putString(AppConstants.BundleKey.WORK_MY_INVITE_ID, applyId)
        })
    })
    val btnMenu3Click: BindingCommand<Void> = BindingCommand(BindingAction {
        uc.showAuditEvent.postValue("2")
    })

    val btnMenu4Click: BindingCommand<Void> = BindingCommand(BindingAction {
        uc.showAuditEvent.postValue("3")
    })

    fun getApplyDetail(applyId: String) {
        this.applyId = applyId
        model.getApplyDetails(applyId)
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<ApplyDetailBean>>() {
                override fun onResult(t: BaseBean<ApplyDetailBean>) {
                    if (t.data == null) return
                    if (t.code == 200) {
//                        bean.set(t.data)
                        guestNo.set(t.data?.guestNo ?: "")
                        auditStatus.set(false)
                        selectCodeStatus.set(false)
                        inviteStatus.set(false)
                        //（1已提交 2审核通过 3驳回 4t通过前撤销 5通过之后撤销 6已完成）
                        approveStatus.set(
                            when (t.data?.approveStatus) {
                                "1" -> {
                                    auditStatus.set(true)
                                    "待审核"
                                }
                                "2" -> {
                                    selectCodeStatus.set(true)
                                    "待拜访"
                                }
                                "3" -> {
                                    "驳回"
                                }
                                "4" -> {
                                    selectCodeStatus.set(true)
                                    "通过前撤销"
                                }
                                "5" -> {
                                    "通过之后撤销"
                                }
                                "6" -> {
                                    selectCodeStatus.set(true)
                                    inviteStatus.set(true)
                                    "已完成"
                                }
                                else -> {
                                    ""
                                }
                            }
                        )

                        if (t.data?.guestType == "1") {
                            selectCodeStatus.set(false)
                            inviteStatus.set(false)
                        } else {
                            auditStatus.set(false)
                        }
                        createTime.set(t.data?.createTime ?: "")
                        reservationTime.set(t.data?.reservationTime ?: "")
                        identity.set(t.data?.identity?:"")
                        guestUserType.set(t.data?.guestUserType ?: "")
                        guestCardType.set(t.data?.guestCardType ?: "")
                        guestName.set(t.data?.guestName ?: "")
                        guestMobile.set(t.data?.guestMobile ?: "")
                        cardNo.set(t.data?.guestVehicleNo ?: "")
                        guestFaceUrl.set(t.data?.guestFaceUrl ?: "")
                        visitName.set(t.data?.visitName ?: "")
                        visitMobile.set(t.data?.visitMobile ?: "")
                        guestPurpose.set(t.data?.guestPurpose ?: "")
                        unitName.set("${t.data?.buildName ?: ""}${t.data?.unitName ?: ""}")
                    } else {
                        showErrorToast(t.msg)
                    }
                }

                override fun onFailed(msg: String?) {
                    showErrorToast(msg)
                }
            })
    }

    fun applyAudit(approveRemark: String, approveAuditStatus: String) {
        model.applyAudit(applyId, approveRemark, approveAuditStatus)
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<String>>() {
                override fun onResult(t: BaseBean<String>) {
                    if (t.code == 200) {
                        LiveBusCenter.postAppointmentListRefreshEvent("1")
                        ToastUtils.showShort("审核成功")
                        finish()
                    } else {
                        showErrorToast(t.msg)
                    }
                }

                override fun onFailed(msg: String?) {
                    showErrorToast(msg)
                }
            })
    }
}